package cn.rc.rcsystem.web.controller;

import cn.rc.rcsystem.dto.CarRentalDto;
import cn.rc.rcsystem.service.ICarRentalService;
import cn.rc.rcsystem.domain.CarRental;
import cn.rc.rcsystem.query.CarRentalQuery;
import cn.rc.rcsystem.result.JSONResult;
import cn.rc.rcsystem.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/carRental")
public class CarRentalController {

    @Autowired
    public ICarRentalService carRentalService;

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody CarRental carRental){
        if(carRental.getId()!=null){
            carRentalService.updateById(carRental);
        }else{
            carRentalService.insert(carRental);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        carRentalService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(carRentalService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(carRentalService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CarRentalQuery query){
        Page<CarRental> page = new Page<CarRental>(query.getPage(),query.getRows());
        page = carRentalService.selectPage(page);
        return JSONResult.success(new PageList<CarRental>(page.getTotal(),page.getRecords()));
    }
    /**
     * 店铺添加租车
     */
    @RequestMapping(value = "/rental",method = RequestMethod.POST)
    public JSONResult rental(@RequestBody CarRental carRental){
        return carRentalService.rental(carRental);
    }
    /**
     * 查询接口
     */
    @RequestMapping(value = "/charter/{shopId}/{carNumber}",method = RequestMethod.POST)
    public JSONResult charter(@PathVariable("shopId")Long shopId ,@PathVariable("carNumber")String carNumber){
        return JSONResult.success(carRentalService.charter(shopId,carNumber));
    }
    /**
     * 租赁车上架
     */
    @RequestMapping(value = "/grounding",method = RequestMethod.POST)
    public JSONResult grounding(@RequestBody CarRentalDto carRentalDto ){
        return carRentalService.grounding(carRentalDto);
    }
}
